<?php

namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\Util;

class CashController extends BaseController
{

    private $userCashLogic;

    public function __construct()
    {
        parent::__construct();
        $this->userCashLogic = D('UserCash', 'Logic');
    }

    public function index()
    {
        $this->display();
    }

    public function cashList()
    {
        $page = I('request.p');
        $pageSize = 10;

        $params = [];
        if (isset($_POST['status'])) {
            $params['status'] = I('request.status/d', 0);
        }
        $params['userId'] = I('request.user_id');
        $params['mobile'] = I('request.mobile');
        $params['realName'] = I('request.real_name');
        $params['icard'] = I('request.icard');
        $params['startTime'] = I('request.start_time', 0, 'strtotime');
        $params['endTime'] = I('request.end_time', 0, 'strtotime');

        $data = $this->userCashLogic->getCashList($page, $pageSize, $params);

        $Page = new AjaxPage($data['total'], $pageSize, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出
        foreach ($data['list'] as &$v) {
            if (strpos($v['remark'], '。')) {
                $arr = explode('。', $v['remark']);
                $v['remark'] = $arr[1];
            }
        }
        $this->assign('total', $data['total']);
        $this->assign('status', $params['status']);
        $this->assign('amount', $data['amount']);
        $this->assign('list', $data['list']);
        $this->assign('page_show', $show);
        $this->display();
    }

    public function withdraw()
    {
        $id = I('get.id');

        $cash = $this->userCashLogic->getUserCashInfo($id);

        $userInfo = D('ClcwUser', 'Logic')->getUserInfoByUid($cash['user_id']);


        $this->assign('user', $userInfo);
        $this->assign('cash', $cash);
        $this->display();


    }

    public function doWithdraw()
    {
        $id = I('post.id');
        $result = I('post.result');
        $remark = I('post.remark');

        $model = M('user_cash', 'clcw_', 'DB_CLCW');
        $cash = $this->userCashLogic->getUserCashInfo($id);
        $userInfo = D('ClcwUser', 'Logic')->getUserInfoByUid($cash['user_id']);

        if (!$cash) {
            die(Util::auto_jsonp_or_json($_REQUEST, ['code' => '0', 'msg' => '提现数据不存在']));
        }

        $model->startTrans();

        if ($result === '1') {
            $remark = $userInfo['real_name'] . ',' . $cash['bank'] . ',' . $cash['card'] . '。提现金额' . $cash['amount'] . '元';
            //修改提现状态
            $res1 = $model->where(['id' => $id])->save(
                [
                    'status'        => 1,
                    'remark'        => $remark,
                    'update_time'   => NOW_TIME,
                    'withdraw_time' => NOW_TIME
                ]
            );
            //修改用户账户冻结金额
            $res2 = M('user', 'clcw_', 'DB_CLCW')->where(['user_id' => $cash['user_id']])
                ->save(
                    [
                        'frost_money' => ($userInfo['frost_money'] - $cash['amount']),
                        'update_time' => NOW_TIME
                    ]
                );
            //添加用户资金记录
            $res3 = M('trade_log', 'clcw_', 'DB_CLCW')->add(
                [
                    'user_id'       => $userInfo['user_id'],
                    'type'          => 61,
                    'before_amount' => $userInfo['user_money'],
                    'action_amount' => $cash['amount'],
                    'after_amount'  => $userInfo['user_money'],
                    'remark'        => $remark,
                    'create_time'   => NOW_TIME
                ]
            );
        } else {
            $remark = $userInfo['real_name'] . ',' . $cash['bank'] . ',' . $cash['card'] . '。' . $remark;
            //修改提现状态
            $res1 = $model->where(['id' => $id])->save(
                [
                    'status'        => 2,
                    'update_time'   => NOW_TIME,
                    'remark'        => $remark,
                    'withdraw_time' => NOW_TIME
                ]
            );
            //解冻账户资金
            $res2 = M('user', 'clcw_', 'DB_CLCW')->where(['user_id' => $cash['user_id']])->save(
                    [
                        'frost_money' => ($userInfo['frost_money'] - $cash['amount']),
                        'user_money'  => $userInfo['user_money'] + $cash['amount'],
                        'update_time' => NOW_TIME
                    ]
                );
            //添加用户资金记录
            $res3 = M('trade_log', 'clcw_', 'DB_CLCW')->add(
                [
                    'user_id'       => $userInfo['user_id'],
                    'type'          => 8,
                    'before_amount' => $userInfo['user_money'],
                    'action_amount' => $cash['amount'],
                    'after_amount'  => $userInfo['user_money'] + $cash['amount'],
                    'remark'        => $remark,
                    'create_time'   => NOW_TIME
                ]
            );
        }

        if ($res1 && $res2 && $res3) {
            $res = true;
            $model->commit();
        } else {
            $res = false;
            $model->rollback();
        }


        if ($res) {
            die(Util::auto_jsonp_or_json($_REQUEST, ['code' => '200', 'msg' => '操作成功']));
        }

        die(Util::auto_jsonp_or_json($_REQUEST, ['code' => 0, 'msg' => '操作失败']));

    }


}